package com.zhang.gmall.realtime.app.dwm;

import com.alibaba.fastjson.JSON;
import com.zhang.gmall.realtime.beans.OrderWide;
import com.zhang.gmall.realtime.beans.PaymentInfo;
import com.zhang.gmall.realtime.beans.PaymentWide;
import com.zhang.gmall.realtime.utils.DateTimeUtil;
import com.zhang.gmall.realtime.utils.MyKafkaUtil;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.restartstrategy.RestartStrategies;
import org.apache.flink.runtime.state.filesystem.FsStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

import java.time.Duration;

/**
 * @title: 支付宽表
 * @author: zhang
 * @date: 2022/3/10 09:55
 * 执行流程
 *      执行模拟业务数据jar
 *      生成数据到业务数据库
 *      binlog记录业务数据库表的操作
 *      maxwell将业务数据库变化的数据封装json字符串发送到kafka ods层 ods_base_db_m 主题
 *      BaseDBApp：读取kafka ods层业务数据，FlinkCDC读取配置表数据转化为广播流，将配置流与广播流连接进行根据配置进行动态分流
 *      OrderWideApp：读取dwd_order_info和dwd_order_detail数据进行双流join，并和相关维度数据进行关联，将订单宽表写入kafka主题dwm_order_wide_2022
 *      PaymentWideApp：读取dwd_payment_info和dwm_order_wide_2022数据进行双流join，将支付宽表写入kafka主题dwm_payment_wide_2022
 *
 */
public class PaymentWideApp {
    public static void main(String[] args) throws Exception {
        //TODO 1.获取环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(4);

        //TODO 2.设置检查点
/*        //设置检查点触发周期和barrier对齐
        env.enableCheckpointing(5000L, CheckpointingMode.EXACTLY_ONCE);
        env.setStateBackend(new FsStateBackend("hdfs://hadoop102:8020/flink/gmall-ck"));
        env.getCheckpointConfig().setCheckpointTimeout(60*1000L);//设置超时时间
        env.getCheckpointConfig().setMinPauseBetweenCheckpoints(1000L);//设置两个ck之间最少等待时间
        //任务cancel时保留ck
        env.getCheckpointConfig().enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);
        //固定延迟重启
        env.setRestartStrategy(RestartStrategies.fixedDelayRestart(3,3000L));
        //设置操作hadoop用户
        System.setProperty("HADOOP_USER_NAME", "zhang");*/

        //TODO 3.读取支付数据封装pojo类，分配时间戳和水位线
        String sourceTopic = "dwd_payment_info";
        String groupId = "dwd_payment_wide_app";
        SingleOutputStreamOperator<PaymentInfo> paymentInfoDS = env
                .addSource(MyKafkaUtil.getKafkaSource(sourceTopic, groupId))
                .map(data -> JSON.parseObject(data, PaymentInfo.class))
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<PaymentInfo>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                                .withTimestampAssigner(new SerializableTimestampAssigner<PaymentInfo>() {
                                    @Override
                                    public long extractTimestamp(PaymentInfo element, long recordTimestamp) {
                                        return DateTimeUtil.toTs(element.getCallback_time());
                                    }
                                })
                );

        //paymentInfoDS.print("paymentInfoDS");
        //TODO 4.读取订单宽表数据封装pojo类，分配时间戳和水位线
        String orderWideTopic = "dwm_order_wide_2022";
        SingleOutputStreamOperator<OrderWide> orderWideDS = env
                .addSource(MyKafkaUtil.getKafkaSource(orderWideTopic, groupId))
                .map(data -> JSON.parseObject(data, OrderWide.class))
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<OrderWide>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                                .withTimestampAssigner(new SerializableTimestampAssigner<OrderWide>() {
                                    @Override
                                    public long extractTimestamp(OrderWide element, long recordTimestamp) {
                                        return DateTimeUtil.toTs(element.getCreate_time());
                                    }
                                })
                );
        //orderWideDS.print("orderWideDS");
        //TODO 5.双流join
        SingleOutputStreamOperator<PaymentWide> paymentWideDS = paymentInfoDS
                .keyBy(PaymentInfo::getOrder_id)
                .intervalJoin(orderWideDS.keyBy(OrderWide::getOrder_id))
                .between(Time.minutes(-15), Time.minutes(5))
                .process(new ProcessJoinFunction<PaymentInfo, OrderWide, PaymentWide>() {
                    @Override
                    public void processElement(PaymentInfo paymentInfo, OrderWide orderWide, ProcessJoinFunction<PaymentInfo, OrderWide, PaymentWide>.Context ctx, Collector<PaymentWide> out) throws Exception {
                        out.collect(new PaymentWide(paymentInfo, orderWide));
                    }
                });
        //TODO 6.写到kafka主题
        //打印测试
        paymentWideDS.map(JSON::toJSONString).print("paymentWideDS");
        paymentWideDS.map(JSON::toJSONString).addSink(MyKafkaUtil.getKafkaSink("dwm_payment_wide_2022"));

        //TODO 7.执行任务
        env.execute("PaymentWideApp");
    }
}
